// pages/modelsOffer/modelsOffer.js
var api = require('../../utils/api.js');
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
      /**
       * topbar
       */
      top_left:'全部品牌',
      top_left_icon: 0,    //左指示图标 【0:黑、1:蓝】
      top_left_sel: 0,    //左图标选中 【0:没选中、1:选中】
      top_right:'默认排序',
      top_right_icon: 0,    //右指示图标 【0:黑、1:蓝】
      top_right_sel: 0,    //右图标选中 【0:没选中、1:选中】
      
      mask_layer_hide: true,   //遮罩层

      curr_show_ver:-1, //显示的品牌系列（-1表示隐藏）
      hide_brand:true,  //品牌隐藏
      hide_sort:true,   //排行隐藏

      brands:[],  //品牌数据
      listData:[],


      //条件默认
      car_id : 0,
      ver_id:0,
      sort_type:0,

      page_star:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**r_
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;

    //获取标签显示  全部品牌、默认排序
    try {   //品牌
      var top_left = wx.getStorageSync('top_left')
      if (top_left) {

        var top_left_icon = (top_left == '全部品牌') ? 0 : 1;
        that.setData({
          top_left: top_left,
          top_left_icon: top_left_icon
        })
      }
    } catch (e) {
    }

    //获取标签显示  全部品牌、默认排序
    try {   //品牌
      var top_right = wx.getStorageSync('top_right')
      if (top_right) {

        var top_right_icon = (top_right != '默认排序') ? 0 : 1;
        that.setData({
          top_right: top_right,
          top_right_icon: top_right_icon
        })
      }
    } catch (e) {
    }

    try {
      var ver_id = wx.getStorageSync('ver_id')
      if (!ver_id) {
        ver_id = 0;
      } 
    } catch (e) {
      ver_id = 0;
    }
    
    try {
      var sort_type = wx.getStorageSync('sort_type')
      if (!sort_type) {
        sort_type = 0;
      }
    } catch (e) {
      sort_type = 0;
    }
    that.setData({
      ver_id: ver_id,
      sort_type: sort_type
    });

    try {
      var brands = wx.getStorageSync('brands')
      if (brands) {
        that.setData({
          brands: brands
        });
      } else {
        that.getBrands();
      }
    } catch (e) {
      that.getBrands();
    }

    //列表
    try {
      var listData = wx.getStorageSync("car_list_" + ver_id + '_sort_' + sort_type)
      if (listData) {
        that.setData({
          listData: listData
        });
      } else {
        that.getListData(ver_id, sort_type);
      }
    } catch (e) {
      that.getListData(ver_id, sort_type);
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

    this.getBrands();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
 
  /**
   * 获取所有品牌列表
   */
  getBrands:function(){
    var that = this;

    api.get({
      url:'portal/categories/index',
      success:function(res){
        console.log(res);
        if(res.code){
          var brands = res.data;

          that.setData({
            brands: brands
          })

          wx.setStorage({
            key: "brands",
            data: brands
          })

        }else{
          wx.showToast({
            title: data.msg,
            icon: 'error',
            duration: 1000
          });
        }
      },
      fail:function(res){

      }
    });
  },

  /**
   * 点击顶部topbar
   */
  topClick:function(e){
    var that = this;

    //左
    var top_left_sel = that.data.top_left_sel;    //左图标选中 【0:没选中、1:选中】
    //右
    var top_right_sel = that.data.top_right_sel;    //左图标选中 【0:没选中、1:选中】

    var target = e.currentTarget.id

    if (target == 'top_left'){

      //只要点击了左边，右侧绝对是非点击状态
      top_right_sel = 0

      var mask_layer_hide = top_left_sel==0?false:true;

      that.setData({
        top_right_sel:top_right_sel,
        top_left_sel: !top_left_sel,
        mask_layer_hide: mask_layer_hide,
        hide_brand: top_left_sel,
        hide_sort:true
      })


    }else{
      //只要点击了右边，左侧绝对是非点击状态
      top_left_sel = 0

      var mask_layer_hide = top_right_sel == 0 ? false : true;
      
      that.setData({
        top_right_sel: !top_right_sel,
        top_left_sel: top_left_sel,
        mask_layer_hide: mask_layer_hide,
        hide_brand: true,
        hide_sort: top_right_sel == true ? true: false         
      })
    }
  },
  /**
   * 点击就隐藏
   */
  hide_mask_layer:function(){
    var that = this;

    //左
    var top_left_sel = that.data.top_left_sel;    //左图标选中 【0:没选中、1:选中】
    //右
    var top_right_sel = that.data.top_right_sel;    //左图标选中 【0:没选中、1:选中】
    var mask_layer_hide = that.data.mask_layer_hide;    //遮罩层

    that.setData({
      top_right_sel: 0,
      top_left_sel: 0,
      mask_layer_hide: true,
      hide_brand: true ,
      hide_sort:true
    })
  },
  //点击品牌类型 显示系列
  clickBrand:function(e){
    var id = e.currentTarget.id;

    this.setData({
      curr_show_ver:id
    })
  },
  //点击“全部品牌”
  allBrand: function (refresh = 1){
    var that = this;
    var cat_type = 0;
    var sort_type = that.data.sort_type;

    //选中了全部品牌，变黑色
    var top_left_icon = 0;
    //左侧文字更改
    var top_left = '全部品牌';

    that.setData({
      top_left_icon: top_left_icon,
      top_left: top_left
    })

    that.getListData(cat_type, sort_type,1);

  },
  /**
   * 点击品牌选中取数据
   */
  getBrandAct: function (e) {
    console.log(e)
    var id = e.currentTarget.id;
    //选中的品牌类型text
    var text = e.currentTarget.dataset.value;

    var that = this;
    var sort_type = that.data.sort_type;

    //选中了品牌项，变蓝色
    var top_left_icon = 1;
    //左侧文字更改
    var top_left = text;

    that.setData({
      top_left_icon: top_left_icon,
      top_left: top_left,
      ver_id:id
    })

    that.getListData(id, sort_type, 1);
  },
  /**
   * 排序
   */
  getDataBySort:function(e){
    console.log(e)
    var id = e.currentTarget.id;  //选中的ID
    var text = e.currentTarget.dataset.value;  //选中文字
    //右侧被选中标志
    var top_right_icon = this.data.top_right_icon;
    
    if(id == 0){ //右侧右被选中的项
      top_right_icon = 0
    }else{
      top_right_icon = 1      
    }

    this.setData({
      sort_type:id,
      top_right: text,
      top_right_icon: top_right_icon
    })

    //cat_type
    var cat_type = this.data.ver_id;

    this.getListData(cat_type, id, 1);

  },
  /**
   * 获取列表
   */
  getListData: function (cat_type, sort_type, refresh = 1){
    var that = this;

    var page_star = that.data.page_star;
    if (refresh){
      page_star = 0;
    }

    var postData = { page_star: page_star, cat_type: cat_type, sort_type: sort_type };

    api.post({
      url: 'portal/car/list',
      data: postData,
      success: function (res) {
        console.log(res)
        if (res.code) {
          //保存data
          var list = res.data;
          //更新page_star
          if (refresh) {
            page_star = list.length;
          } else {
            page_star += list.length;
          }
          var newList = (refresh == 1) ? list : that.data.listData.concat(list);

          //记录选中状态
          

          that.setData({
            listData: newList,
            page_star: page_star,
            top_right_sel: 0,
            top_left_sel: 0,
            mask_layer_hide: true,
            hide_brand: true,
            hide_sort: true
          })

          //缓存
          wx.setStorage({
            key: "car_list_" + cat_type + '_sort_' + sort_type,
            data: list
          })
          //更新ver_id、sort
          wx.setStorage({
            key: "ver_id",
            data: cat_type
          })
          wx.setStorage({
            key: "sort_type",
            data: sort_type
          })


          wx.showToast({
            title: res.msg,
            icon: 'success',
            duration: 1000
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'error',
            duration: 1000
          });
        }

      },
      fail: function (res) {

      }
    })
  },
  /**
   * 跳转详情页
   */
  nav_detail:function(e){
    console.log(e)
    var form_id = e.detail.formId;
    //保存form_id
    app.cacheFormIDs(form_id);

    var url = e.target.dataset.url;
    console.log(url)
    wx.navigateTo({
      url: url,
    })
  }
})